package org.jeecg.talentCooperation.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    /**
     * 提取字符串中的所有符号
     * @param str
     * @return
     */
    public static String getSymbol (String str) {
        String chinese = "[\\u4e00-\\u9fa5\\w\\d]";//汉字字符集+英文字符集+数字字符集
        Pattern p = Pattern.compile(chinese);
        Matcher m = p.matcher(str);
        return m.replaceAll("");
    }

    /**
     * 去除字符串中的空格、回车、换行符、制表符
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 全是汉字的姓名，去除字符串中的空格、回车、换行符、制表符
     * @param str
     * @return
     */
    public static String replaceBlankOfName(String str) {
        String result = "";
        StringBuilder sb = new StringBuilder();
        char[] chars = str.toCharArray();
        for (char c : chars) {
            if (32 == c || 12288 == c) {
                continue;
            }
            sb.append(c);
        }
        String value = sb.toString();

        //判断全是汉字
        String reg = "[\\u4e00-\\u9fa5]+";
        if(value.matches(reg)){
            result = value;
        }else{
            result = str;
        }
        return result;
    }

    /**
     * 是否包含特殊符号
     * @param str
     * @return
     */
    public static boolean isSpecialChar(String str) {
        String regEx = "[ _`[email protected]#$%^&*()+=|{}‘:;‘,\\[\\].<>/?~！@#￥%……&*()——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 是否包含数字
     * @param str
     * @return
     */
    public static boolean isContainNumber(String str) {
        Pattern compile = Pattern.compile(".*[0-9].*");
        Matcher matcher = compile.matcher(str);
        return matcher.find();
    }

    /**
     * 判断是否是日期
     * @return
     */
    public static boolean isDateValid(String date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            sdf.parse(date);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String sqls = "碳达峰碳中和研究：碳达峰、碳中和、双碳\n" +
                "新型电力系统研究：新型电力系统、电力系统\n" +
                "能源电力战略规划研究：规划研究、能源战略、电力战略\n" +
                "能源电力供需研究：能源供需、电力供需、供需研究\n" +
                "能源电力体制改革与电力市场研究：能源体制、电力体制、体制改革、电力市场\n" +
                "能源电力数字经济研究：数字经济、能源经济、电力经济\n" +
                "企业战略规划研究：战略规划、战略规划研究\n" +
                "企业经营与财务研究：企业经营、财务\n" +
                "企业运营管理研究：企业运营、运营管理\n" +
                "企业治理与风控合规研究：企业治理、风控合规\n" +
                "品牌文化与社会责任研究：品牌文化、社会责任\n" +
                "数据、模型工具及实验室：数据、模型工具、实验室";

        String[] sqlArr = sqls.split("\n");
        for (String s : sqlArr) {
            String[] rf = s.split("：");
            String rfName = rf[0];
            String[] rfKey = rf[1].split("、");
            for (String key : rfKey) {
//                System.out.println("\n" +
//                        "SELECT nf.* from nd_fruit nf  where nf.name like \"%"+key+"%\"  or nf.name like \"%"+key+"%\" or nf.name like \"%"+key+"%\" \n" +
//                        " or nf.keyword like \"%"+key+"%\"  or nf.name like \"%"+key+"%\" or nf.name like \"%"+key+"%\"\n" +
//                        " or nf.summary like \"%"+key+"%\"  or nf.summary like \"%"+key+"%\" or nf.summary like \"%"+key+"%\"\n" +
//                        " or nf.field like \"%"+key+"%\"  or nf.field like \"%"+key+"%\" or nf.field like \"%"+key+"%\";");
                System.out.println("\n" +
                        "update nd_fruit nf set nf.research_field = '"+rfName+"' where nf.name like \"%"+key+"%\"  or nf.name like \"%"+key+"%\" or nf.name like \"%"+key+"%\" \n" +
                        " or nf.keyword like \"%"+key+"%\"  or nf.name like \"%"+key+"%\" or nf.name like \"%"+key+"%\"\n" +
                        " or nf.summary like \"%"+key+"%\"  or nf.summary like \"%"+key+"%\" or nf.summary like \"%"+key+"%\"\n" +
                        " or nf.field like \"%"+key+"%\"  or nf.field like \"%"+key+"%\" or nf.field like \"%"+key+"%\";");
            }
        }
    }
}
